package com.le.tester.imooc.oldfairy.coding.collection;

import lombok.var;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

/**
 * createTime：2023/4/25 10:48
 * description：gen 1000000 data,HashSet VS TreeSet
 */
public class HashSetVSTreeSet {

    @Test
    public void testOrder() {
        Set<Integer> hashSet = new HashSet<>();
        hashSet.add(3);
        hashSet.add(7);
        hashSet.add(2);
        hashSet.add(81);
        System.out.println(hashSet.stream().map(Object::toString).collect(Collectors.joining(",")));

        Set<Integer> treeSet = new TreeSet<>();
        treeSet.add(3);
        treeSet.add(7);
        treeSet.add(2);
        treeSet.add(81);
        System.out.println(treeSet.stream().map(Object::toString).collect(Collectors.joining(",")));

    }

    @Test
    //time 1000000
    public void testBenchmark() {

        Random random = new Random(47);

        LinkedList<String> words = new LinkedList<>();

        for (int i = 0; i < 1000000; i++) {
            //在这个范围进行随机
            String sb = random.ints(97, 124)
                    .limit(12)
                    .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
                    .toString();

            words.add(sb);
        }

        Set<String> hashSet = new HashSet<>();
        Set<String> treeSet = new TreeSet<>();

        long startTime = System.currentTimeMillis();
        for (String s : words) {
            hashSet.add(s);
        }

        for (String s : words) {
            hashSet.contains(s);
        }

        System.out.println("hashSet time is " + (System.currentTimeMillis() - startTime));

        startTime = System.currentTimeMillis();
        for (String s : words) {
            treeSet.add(s);
        }
        for (String s : words) {
            treeSet.contains(s);
        }
        System.out.println("treeSet time is " + (System.currentTimeMillis() - startTime));

    }
}
